package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.main.constant.PreventDuplication;
import com.tbit.main.constant.enums.DispatchApplyHandleType;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.DispatchApplyLog;
import com.tbit.main.pojo.DispatchApplyMachine;
import com.tbit.main.pojo.VO.DispatchApplyLogVo;
import com.tbit.main.req.AddDispatchApplyBatchReq;
import com.tbit.main.service.AccountUserService;
import com.tbit.main.service.DispatchApplyLogService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

/**
 * 调度申请
 *
 * @Author: chen
 * @Description:
 * @Date: 创建于 17:35 2023/2/10
 */
@RestController
@RequestMapping("/dispatchApplyLog")
public class DispatchApplyLogController {

    @Autowired
    private DispatchApplyLogService dispatchApplyLogService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private AccountUserService accountUserService;

    /**
     * 分组统计用户调度申请次数分布
     *
     * @param accountId 区域id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @RequestMapping("/getApplyLogStat")
    public JsonResult getApplyLogStat(String token, Integer accountId, Long startTime, Long endTime) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        Assert.notNull(startTime, "System.startTime.notNull");
        Assert.notNull(endTime, "System.endTime.notNull");
        AccountUser accountUserToken = tokenService.getAccount(token);
        List<DispatchApplyLogVo> dispatchApplyLogVos = dispatchApplyLogService.getApplyLogStat(accountUserToken, accountId, startTime, endTime);
        return JsonResult.succ(dispatchApplyLogVos);
    }

    /**
     * 查询指定运维人员调度申请记录
     *
     * @param accountUserId 运维用户id
     * @param startTime     开始时间
     * @param endTime       结束时间
     * @param mapType       地图类型
     * @return
     */
    @RequestMapping("/getByIdAndTime")
    public JsonResult getByIdAndTime(Integer accountUserId, Long startTime, Long endTime, @RequestParam(defaultValue = "2") Integer mapType, Integer accountId) {
        Assert.notNull(accountUserId, "AccountUserId.notNull");
        Assert.notNull(startTime, "System.startTime.notNull");
        Assert.notNull(endTime, "System.endTime.notNull");
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        List<DispatchApplyLog> dispatchApplyLogs = dispatchApplyLogService.getByIdAndTime(accountUserId, startTime, endTime, mapType, accountId);
        return JsonResult.succ(dispatchApplyLogs);
    }


    /**
     * 通过申请id查询
     *
     * @param applyId 申请记录id
     * @return
     */
    @RequestMapping("/getByApplyId")
    public JsonResult getByApplyId(Integer applyId) {
        Assert.notNull(applyId, "apply.applyId.not.null");
        List<DispatchApplyMachine> dispatchApplyMachines = dispatchApplyLogService.getByApplyId(applyId);
        return JsonResult.succ(dispatchApplyMachines);
    }

    /**
     * 申请
     *
     * @param req      请求参数
     * @param token    token
     */
    @PostMapping("/addBatch")
    @PreventDuplication(expireSeconds = 3)
    public JsonResult addBatch(@RequestBody @Valid AddDispatchApplyBatchReq req,@RequestParam String token) throws Exception {
        AccountUser accountUser = tokenService.getAccount(token);
        List<String> userCode = req.getUserCode();
        String reason = req.getReason();
        Double lon = req.getLon();
        Double lat = req.getLat();
        Integer mapType = req.getMapType();
        List<String> photourl = req.getPhotoUrl();
        dispatchApplyLogService.addBatch(userCode, reason, lon, lat, accountUser, mapType,photourl, req.isNewVersion());
        return JsonResult.succ();
    }


    /**
     * 申请
     * 兼容旧版本 GET请求
     * @param userCode 车辆编号集合
     * @param reason   申请理由
     * @param lon      经度
     * @param lat      纬度
     * @param token    token
     * @param mapType  地图类型
     */
    @GetMapping("/addBatch")
    @PreventDuplication(expireSeconds = 3)
    public JsonResult addBatch(@RequestParam("userCodes") List<String> userCode, String reason, Double lon, Double lat, String token, @RequestParam(defaultValue = "2") Integer mapType) throws Exception {
        Assert.notNull(userCode, "Machine.userCode.notNull");
        Assert.hasLength(reason, "apply.reason.not.null");
        Assert.notNull(lon, "Map.lon.nutNull");
        Assert.notNull(lat, "Map.lat.nutNull");
        AccountUser accountUser = tokenService.getAccount(token);
        dispatchApplyLogService.addBatch(userCode, reason, lon, lat, accountUser, mapType, null, false);
        return JsonResult.succ();
    }

    /**
     * 处理
     *
     * @param applyId 申请记录id
     * @param state   状态
     * @param remark  审核备注
     */
    @RequestMapping("/handle")
    public JsonResult handle(Integer applyId, Integer state, String remark, String token, Integer accountUserIdManager) {
        Assert.notNull(applyId, "apply.applyId.not.null");
        Assert.notNull(state, "apply.state.not.null");
        AccountUser accountUser = new AccountUser();
        if (accountUserIdManager == null) {
            accountUser = tokenService.getAccount(token);
        } else {
            accountUser = accountUserService.getById(accountUserIdManager);
        }
        dispatchApplyLogService.handle(applyId, state, remark, accountUser, DispatchApplyHandleType.MANUAL);
        return JsonResult.succ();
    }

    /**
     * 通过申请id查询
     *
     * @param applyId 申请记录id
     * @return
     */
    @RequestMapping("/getByApplyIdDispatch")
    public JsonResult getByApplyIdDispatch(Integer applyId) {
        Assert.notNull(applyId, "apply.applyId.not.null");
        List<DispatchApplyLog> dispatchApplyLogs = dispatchApplyLogService.getByApplyIdDispatch(applyId);
        return JsonResult.succ(dispatchApplyLogs);
    }

    /**
     * 查询指定运维人员可调度数量
     *
     *  OCCUPY_NO_LIMIT = -999 无限制
     * @param token token
     * @param accountId 区域id
     * @return 可调度数量
     */
    @PostMapping("/checkMachineOccupy")
    public JsonResult<Integer> checkMachineOccupy(@RequestParam String token,@RequestParam Integer accountId){
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        return JsonResult.succ(dispatchApplyLogService.checkMachineOccupy(tokenService.getAccount(token),accountId));
    }
}
